//
//  FilterView.m
//  Fireball
//
//  Created by 任长平 on 2017/12/2.
//  Copyright © 2017年 任长平. All rights reserved.
//

#import "FilterView.h"
#import "HomeViewController.h"

@interface FilterView ()
@property(nonatomic, strong)UIView *contentView;
@property(nonatomic, strong)UIButton *editButton;

@end

@implementation FilterView

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.aticleArray = [NSMutableArray array];
        self.selectedArray = [NSMutableArray array];

        self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:nil];
        self.segmentedControl.frame = CGRectMake(0, 0, 300, 44);
        self.segmentedControl.backgroundColor = [UIColor clearColor];
        self.segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 20, 0, 0);
        
        self.segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor xm_colorFromRGB:0x858C96],NSFontAttributeName:[UIFont xm_font:15.f]};
        self.segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor xm_colorFromRGB:0x212832],NSFontAttributeName:[UIFont xm_medium:15.f]};
        self.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;//设定线条的位置
        self.segmentedControl.selectionIndicatorColor = [UIColor xm_MainColor];
        self.segmentedControl.selectionIndicatorHeight = 3.f;
        [self.segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
        
        [self addSubview:self.segmentedControl];
        
        self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.editButton.backgroundColor = [UIColor redColor];
        [self.editButton addTarget:self action:@selector(editButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.editButton];


        
        [FBNetworkTool get:@"articleapi/com.GetAticleMenu" parameters:@{@"iType":@"0"} showHUD:NO responseCache:^(XMHttpResponseModel *responseModel) {
            if (responseModel.code == 200) {
                NSArray * array = [AticleMenu mj_objectArrayWithKeyValuesArray:responseModel.data];
                [self setSegmentedControlTitle:array];
            }
  
        } success:^(XMHttpResponseModel *responseModel) {
            if (responseModel.code == 200) {
                NSArray * array = [AticleMenu mj_objectArrayWithKeyValuesArray:responseModel.data];
                [self setSegmentedControlTitle:array];
            }
            
        } failure:^(NSError *error) {
            
        }];

        
    }
    return self;
}

-(void)editButtonClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(filterViewEditButtonClick:)]) {
        [self.delegate filterViewEditButtonClick:sender];
    }
}
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(segmentedControlChangedValue:aticleMenu:)]) {
        [self.delegate segmentedControlChangedValue:segmentedControl.selectedSegmentIndex
                                         aticleMenu:self.aticleArray[segmentedControl.selectedSegmentIndex]];
    }
}
-(void)setSegmentedControlTitle:(NSArray *)titles{
    [self.aticleArray removeAllObjects];
    [self.selectedArray removeAllObjects];
    
    AticleMenu * menu1 = [[AticleMenu alloc]init];
    menu1.editable = NO;
    menu1.menuId = 0;
    menu1.Text = @"关注";
    
    AticleMenu * menu2 = [[AticleMenu alloc]init];
    menu2.editable = NO;
    menu2.menuId = 1;
    menu2.Text = @"推荐";
    
    [self.aticleArray addObject:menu1];
    [self.aticleArray addObject:menu2];
    
    [self.aticleArray addObjectsFromArray:titles];
    for (NSInteger i = 0; i < self.aticleArray.count; i ++) {
        AticleMenu * menu = self.aticleArray[i];
        if (i <= 8) {
            menu.selected = YES;
            [self.selectedArray addObject:menu.Text];
        }else{
            menu.selected = NO;
        }
    }
//    [self.homeViewController.collectionView reloadData];
    self.segmentedControl.sectionTitles = self.selectedArray;
}




-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = [UIColor xm_colorFromRGB:0xF7F8FA];
        [self.viewController.view addSubview:_contentView];
        [self.viewController.view bringSubviewToFront:_contentView];
    }
    return _contentView;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.editButton.frame = CGRectMake(self.width - 44, 0, 44, self.height);
    self.segmentedControl.frame = CGRectMake(0, 0, self.width - 44, self.height);
    self.contentView.frame = CGRectMake(0, 44, self.width, 0);
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
}


@end
